package org.example.order.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author tds
 * @version 1.0.0
 * @Company
 * @ClassName LoadBalancerConfiguration
 * @Description 负载均衡配置。这里开启了负载均衡，并且通过注解@LoadBalancerClients针对不同服务使用了不同的负载均衡器
 * @Date 2023-04-16 11:27
 */
@Configuration
@LoadBalancerClients({
        @LoadBalancerClient(name = "stock", configuration = RandomLoadbalancerConfiguration.class),
        @LoadBalancerClient(name = "order", configuration = MyLoadbalancerConfiguration.class)
})
public class LoadBalancerConfiguration {

    @Bean
    // 服务间通过服务名称调用需要用到负载均衡器
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
}
